package com.xxx.video.mine.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.mine.beans.UploadFileBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.io.File;

public class AddVideoPresenter extends BasePresenter<IAddVideoView> {

    public AddVideoPresenter(@NonNull Context context, IAddVideoView baseView) {
        super(context, baseView);
    }

    /**
     * submit
     *
     * @param videoDuration time
     */
    public void submit(long videoDuration) {
        String videoPath = getBaseView() == null ? "" : getBaseView().getVideoPath();
        String videoCoverPath = getBaseView() == null ? "" : getBaseView().getVideoCoverPath();
        String videoTitle = getBaseView() == null ? "" : getBaseView().getVideoTitle();
        if (TextUtils.isEmpty(videoPath)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_video_video_path_can_not_empty));
            return;
        }
        if (TextUtils.isEmpty(videoCoverPath)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_video_video_cover_path_can_not_empty));
            return;
        }
        if (TextUtils.isEmpty(videoTitle)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_video_video_title_can_not_empty));
            return;
        }
        showProgressDialog("loading");
        uploadVideoPath(videoPath, videoCoverPath, videoTitle, videoDuration);
    }

    /**
     * upload video path
     *
     * @param videoPath      video path
     * @param videoCoverPath video cover path
     * @param videoTitle     video title
     * @param videoDuration  time
     */
    public void uploadVideoPath(String videoPath, String videoCoverPath, String videoTitle, long videoDuration) {
        NetWorkUtils.getInstance().uploadFile(new File(videoPath), new CommonResponseListener<UploadFileBean>() {
            @Override
            public void onSuccess(UploadFileBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    uploadVideoCoverPath(successOb.url, videoCoverPath, videoTitle, videoDuration);
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_video_video_path_upload_error));
            }
        });
    }

    /**
     * upload video cover path
     *
     * @param videoPath      video path
     * @param videoCoverPath video cover path
     * @param videoTitle     video title
     * @param videoDuration  time
     */
    public void uploadVideoCoverPath(String videoPath, String videoCoverPath, String videoTitle, long videoDuration) {
        NetWorkUtils.getInstance().uploadFile(new File(videoCoverPath), new CommonResponseListener<UploadFileBean>() {
            @Override
            public void onSuccess(UploadFileBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    release(videoPath, successOb.url, videoTitle, videoDuration);
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_video_video_cover_path_upload_error));
            }
        });
    }

    /**
     * release
     *
     * @param videoPath      video path
     * @param videoCoverPath video cover path
     * @param videoTitle     video title
     * @param videoDuration  time
     */
    public void release(String videoPath, String videoCoverPath, String videoTitle, long videoDuration) {
        NetWorkUtils.getInstance().release(videoPath, videoCoverPath, videoTitle, "1", String.valueOf(videoDuration / 1000), new CommonResponseListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean successOb, String result) {
                hideProgressDialog();
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().releaseSuccess();
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_video_error));
            }
        });
    }

}
